package cn.yunhe.util;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;


/**
 * 外部资源加载类，可以从jar中读取内容
 * @author wjh
 */
public class ReadResourceUtil {

    static File jarFile;

    static {
        try {
            jarFile = new File(java.net.URLDecoder.decode(ReadResourceUtil.class.getProtectionDomain().getCodeSource().getLocation().getFile(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取音频
     */
    public static URL readAudio(String name){
        return getUri(name);
    }

    public static URL getUri(String name){
        URL resource = ReadResourceUtil.class.getResource(name);
        if(resource==null){
            try {
                File file = new File(name);
                resource = file.toURL();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
        return resource;
    }
    
}
